# Copyright (c) 2014-present, The osquery authors
#
# This source code is licensed as defined by the LICENSE file found in the
# root directory of this source tree.
#
# SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)

function(berkeleydbMain)
  set(library_root "${CMAKE_CURRENT_SOURCE_DIR}/src")

  add_library(thirdparty_berkeley-db
    "${library_root}/src/crypto/aes_method.c"
    "${library_root}/src/btree/bt_compact.c"
    "${library_root}/src/btree/bt_compare.c"
    "${library_root}/src/btree/bt_compress.c"
    "${library_root}/src/btree/bt_conv.c"
    "${library_root}/src/btree/bt_curadj.c"
    "${library_root}/src/btree/bt_cursor.c"
    "${library_root}/src/btree/bt_delete.c"
    "${library_root}/src/btree/bt_method.c"
    "${library_root}/src/btree/bt_open.c"
    "${library_root}/src/btree/bt_put.c"
    "${library_root}/src/btree/bt_rec.c"
    "${library_root}/src/btree/bt_reclaim.c"
    "${library_root}/src/btree/bt_recno.c"
    "${library_root}/src/btree/btree_auto.c"
    "${library_root}/src/btree/btree_autop.c"
    "${library_root}/src/btree/bt_rsearch.c"
    "${library_root}/src/btree/bt_search.c"
    "${library_root}/src/btree/bt_split.c"
    "${library_root}/src/btree/bt_stat.c"
    "${library_root}/src/btree/bt_upgrade.c"
    "${library_root}/src/btree/bt_verify.c"
    "${library_root}/src/common/clock.c"
    "${library_root}/src/db/crdel_auto.c"
    "${library_root}/src/db/crdel_autop.c"
    "${library_root}/src/db/crdel_rec.c"
    "${library_root}/src/crypto/crypto.c"
    "${library_root}/lang/db185/db185.c"
    "${library_root}/src/db/db_am.c"
    "${library_root}/src/db/db_auto.c"
    "${library_root}/src/db/db_autop.c"
    "${library_root}/src/db/db_backup.c"
    "${library_root}/src/common/db_byteorder.c"
    "${library_root}/src/db/db.c"
    "${library_root}/src/db/db_cam.c"
    "${library_root}/src/db/db_cds.c"
    "${library_root}/src/db/db_compact.c"
    "${library_root}/src/common/db_compint.c"
    "${library_root}/src/db/db_conv.c"
    "${library_root}/src/db/db_copy.c"
    "${library_root}/src/db/db_dispatch.c"
    "${library_root}/src/db/db_dup.c"
    "${library_root}/src/common/db_err.c"
    "${library_root}/src/common/db_getlong.c"
    "${library_root}/src/common/db_idspace.c"
    "${library_root}/src/db/db_iface.c"
    "${library_root}/src/db/db_join.c"
    "${library_root}/src/common/db_log2.c"
    "${library_root}/src/db/db_meta.c"
    "${library_root}/src/db/db_method.c"
    "${library_root}/src/db/db_open.c"
    "${library_root}/src/db/db_overflow.c"
    "${library_root}/src/db/db_ovfl_vrfy.c"
    "${library_root}/src/db/db_pr.c"
    "${library_root}/src/db/db_rec.c"
    "${library_root}/src/db/db_reclaim.c"
    "${library_root}/src/dbreg/dbreg_auto.c"
    "${library_root}/src/dbreg/dbreg_autop.c"
    "${library_root}/src/dbreg/dbreg.c"
    "${library_root}/src/dbreg/dbreg_rec.c"
    "${library_root}/src/dbreg/dbreg_stat.c"
    "${library_root}/src/dbreg/dbreg_util.c"
    "${library_root}/src/db/db_remove.c"
    "${library_root}/src/db/db_rename.c"
    "${library_root}/src/db/db_ret.c"
    "${library_root}/src/db/db_setid.c"
    "${library_root}/src/db/db_setlsn.c"
    "${library_root}/src/common/db_shash.c"
    "${library_root}/src/db/db_sort_multiple.c"
    "${library_root}/src/db/db_stati.c"
    "${library_root}/src/common/dbt.c"
    "${library_root}/src/db/db_truncate.c"
    "${library_root}/src/db/db_upg.c"
    "${library_root}/src/db/db_upg_opd.c"
    "${library_root}/src/db/db_vrfy.c"
    "${library_root}/src/db/db_vrfyutil.c"
    "${library_root}/src/env/env_alloc.c"
    "${library_root}/src/env/env_backup.c"
    "${library_root}/src/env/env_config.c"
    "${library_root}/src/env/env_failchk.c"
    "${library_root}/src/env/env_file.c"
    "${library_root}/src/env/env_globals.c"
    "${library_root}/src/env/env_method.c"
    "${library_root}/src/env/env_name.c"
    "${library_root}/src/env/env_open.c"
    "${library_root}/src/env/env_recover.c"
    "${library_root}/src/env/env_region.c"
    "${library_root}/src/env/env_register.c"
    "${library_root}/src/env/env_sig.c"
    "${library_root}/src/env/env_stat.c"
    "${library_root}/src/fileops/fileops_auto.c"
    "${library_root}/src/fileops/fileops_autop.c"
    "${library_root}/src/fileops/fop_basic.c"
    "${library_root}/src/fileops/fop_rec.c"
    "${library_root}/src/fileops/fop_util.c"
    "${library_root}/src/hash/hash_auto.c"
    "${library_root}/src/hash/hash_autop.c"
    "${library_root}/src/hash/hash.c"
    "${library_root}/src/hash/hash_compact.c"
    "${library_root}/src/hash/hash_conv.c"
    "${library_root}/src/hash/hash_dup.c"
    "${library_root}/src/hash/hash_func.c"
    "${library_root}/src/hash/hash_meta.c"
    "${library_root}/src/hash/hash_method.c"
    "${library_root}/src/hash/hash_open.c"
    "${library_root}/src/hash/hash_page.c"
    "${library_root}/src/hash/hash_rec.c"
    "${library_root}/src/hash/hash_reclaim.c"
    "${library_root}/src/hash/hash_stat.c"
    "${library_root}/src/hash/hash_upgrade.c"
    "${library_root}/src/hash/hash_verify.c"
    "${library_root}/src/heap/heap_auto.c"
    "${library_root}/src/heap/heap_autop.c"
    "${library_root}/src/heap/heap_backup.c"
    "${library_root}/src/heap/heap.c"
    "${library_root}/src/heap/heap_conv.c"
    "${library_root}/src/heap/heap_method.c"
    "${library_root}/src/heap/heap_open.c"
    "${library_root}/src/heap/heap_rec.c"
    "${library_root}/src/heap/heap_reclaim.c"
    "${library_root}/src/heap/heap_stat.c"
    "${library_root}/src/heap/heap_verify.c"
    "${library_root}/src/hmac/hmac.c"
    "${library_root}/src/hmac/sha1.c"
    "${library_root}/src/lock/lock.c"
    "${library_root}/src/lock/lock_deadlock.c"
    "${library_root}/src/lock/lock_failchk.c"
    "${library_root}/src/lock/lock_id.c"
    "${library_root}/src/lock/lock_list.c"
    "${library_root}/src/lock/lock_method.c"
    "${library_root}/src/lock/lock_region.c"
    "${library_root}/src/lock/lock_stat.c"
    "${library_root}/src/lock/lock_timer.c"
    "${library_root}/src/lock/lock_util.c"
    "${library_root}/src/log/log_archive.c"
    "${library_root}/src/log/log.c"
    "${library_root}/src/log/log_compare.c"
    "${library_root}/src/log/log_debug.c"
    "${library_root}/src/log/log_get.c"
    "${library_root}/src/log/log_method.c"
    "${library_root}/src/log/log_print.c"
    "${library_root}/src/log/log_put.c"
    "${library_root}/src/log/log_stat.c"
    "${library_root}/src/log/log_verify_auto.c"
    "${library_root}/src/log/log_verify.c"
    "${library_root}/src/log/log_verify_int.c"
    "${library_root}/src/log/log_verify_util.c"
    "${library_root}/src/common/mkpath.c"
    "${library_root}/src/mp/mp_alloc.c"
    "${library_root}/src/mp/mp_backup.c"
    "${library_root}/src/mp/mp_bh.c"
    "${library_root}/src/mp/mp_fget.c"
    "${library_root}/src/mp/mp_fmethod.c"
    "${library_root}/src/mp/mp_fopen.c"
    "${library_root}/src/mp/mp_fput.c"
    "${library_root}/src/mp/mp_fset.c"
    "${library_root}/src/mp/mp_method.c"
    "${library_root}/src/mp/mp_mvcc.c"
    "${library_root}/src/mp/mp_region.c"
    "${library_root}/src/mp/mp_register.c"
    "${library_root}/src/mp/mp_resize.c"
    "${library_root}/src/mp/mp_stat.c"
    "${library_root}/src/mp/mp_sync.c"
    "${library_root}/src/mp/mp_trickle.c"
    "${library_root}/src/crypto/mersenne/mt19937db.c"
    "${library_root}/src/crypto/rijndael/rijndael-api-fst.c"
    "${library_root}/src/crypto/rijndael/rijndael-alg-fst.c"
    "${library_root}/src/mutex/mut_alloc.c"
    "${library_root}/src/mutex/mut_failchk.c"
    "${library_root}/src/mutex/mut_method.c"
    "${library_root}/src/mutex/mut_pthread.c"
    "${library_root}/src/mutex/mut_region.c"
    "${library_root}/src/mutex/mut_stat.c"
    "${library_root}/src/common/openflags.c"
    "${library_root}/src/os/os_abort.c"
    "${library_root}/src/os/os_abs.c"
    "${library_root}/src/os/os_addrinfo.c"
    "${library_root}/src/os/os_alloc.c"
    "${library_root}/src/os/os_clock.c"
    "${library_root}/src/os/os_config.c"
    "${library_root}/src/os/os_cpu.c"
    "${library_root}/src/os/os_ctime.c"
    "${library_root}/src/os/os_dir.c"
    "${library_root}/src/os/os_errno.c"
    "${library_root}/src/os/os_fid.c"
    "${library_root}/src/os/os_flock.c"
    "${library_root}/src/os/os_fsync.c"
    "${library_root}/src/os/os_getenv.c"
    "${library_root}/src/os/os_handle.c"
    "${library_root}/src/os/os_map.c"
    "${library_root}/src/common/os_method.c"
    "${library_root}/src/os/os_mkdir.c"
    "${library_root}/src/os/os_open.c"
    "${library_root}/src/os/os_path.c"
    "${library_root}/src/os/os_pid.c"
    "${library_root}/src/os/os_rename.c"
    "${library_root}/src/os/os_root.c"
    "${library_root}/src/os/os_rpath.c"
    "${library_root}/src/os/os_rw.c"
    "${library_root}/src/os/os_seek.c"
    "${library_root}/src/os/os_stack.c"
    "${library_root}/src/os/os_stat.c"
    "${library_root}/src/os/os_tmpdir.c"
    "${library_root}/src/os/os_truncate.c"
    "${library_root}/src/os/os_uid.c"
    "${library_root}/src/os/os_unlink.c"
    "${library_root}/src/os/os_yield.c"
    "${library_root}/src/db/partition.c"
    "${library_root}/src/qam/qam_auto.c"
    "${library_root}/src/qam/qam_autop.c"
    "${library_root}/src/qam/qam.c"
    "${library_root}/src/qam/qam_conv.c"
    "${library_root}/src/qam/qam_files.c"
    "${library_root}/src/qam/qam_method.c"
    "${library_root}/src/qam/qam_open.c"
    "${library_root}/src/qam/qam_rec.c"
    "${library_root}/src/qam/qam_stat.c"
    "${library_root}/src/qam/qam_upgrade.c"
    "${library_root}/src/qam/qam_verify.c"
    "${library_root}/src/rep/rep_automsg.c"
    "${library_root}/src/rep/rep_backup.c"
    "${library_root}/src/rep/rep_elect.c"
    "${library_root}/src/rep/rep_lease.c"
    "${library_root}/src/rep/rep_log.c"
    "${library_root}/src/rep/rep_method.c"
    "${library_root}/src/repmgr/repmgr_sel.c"
    "${library_root}/src/repmgr/repmgr_elect.c"
    "${library_root}/src/repmgr/repmgr_auto.c"
    "${library_root}/src/repmgr/repmgr_automsg.c"
    "${library_root}/src/repmgr/repmgr_autop.c"
    "${library_root}/src/repmgr/repmgr_method.c"
    "${library_root}/src/repmgr/repmgr_msg.c"
    "${library_root}/src/repmgr/repmgr_net.c"
    "${library_root}/src/repmgr/repmgr_posix.c"
    "${library_root}/src/repmgr/repmgr_queue.c"
    "${library_root}/src/repmgr/repmgr_rec.c"
    "${library_root}/src/repmgr/repmgr_stat.c"
    "${library_root}/src/repmgr/repmgr_util.c"
    "${library_root}/src/rep/rep_record.c"
    "${library_root}/src/rep/rep_region.c"
    "${library_root}/src/rep/rep_stat.c"
    "${library_root}/src/rep/rep_util.c"
    "${library_root}/src/rep/rep_verify.c"
    "${library_root}/src/sequence/seq_stat.c"
    "${library_root}/src/sequence/sequence.c"
    "${library_root}/src/clib/snprintf.c"
    "${library_root}/src/txn/txn_auto.c"
    "${library_root}/src/txn/txn_autop.c"
    "${library_root}/src/txn/txn.c"
    "${library_root}/src/txn/txn_chkpt.c"
    "${library_root}/src/txn/txn_failchk.c"
    "${library_root}/src/txn/txn_method.c"
    "${library_root}/src/txn/txn_rec.c"
    "${library_root}/src/txn/txn_recover.c"
    "${library_root}/src/txn/txn_region.c"
    "${library_root}/src/txn/txn_stat.c"
    "${library_root}/src/txn/txn_util.c"
    "${library_root}/src/common/util_cache.c"
    "${library_root}/src/common/util_log.c"
    "${library_root}/src/common/util_sig.c"
    "${library_root}/src/xa/xa.c"
    "${library_root}/src/xa/xa_map.c"
    "${library_root}/src/common/zerofill.c"
  )

  if(TARGET_PROCESSOR STREQUAL "x86_64")
    target_sources(thirdparty_berkeley-db PRIVATE
      "${library_root}/src/mutex/mut_tas.c")
  endif()


  target_compile_definitions(thirdparty_berkeley-db PRIVATE
    _GNU_SOURCE
    _REENTRANT
  )
  if(TARGET_PROCESSOR STREQUAL "x86_64")
    target_compile_definitions(thirdparty_berkeley-db PRIVATE
      __atomic_compare_exchange=__atomic_compare_exchange_db)
  endif()

  target_link_libraries(thirdparty_berkeley-db PUBLIC
    thirdparty_sqlite
  )

  target_link_libraries(thirdparty_berkeley-db PRIVATE
    thirdparty_c_settings
  )

  target_include_directories(thirdparty_berkeley-db PRIVATE
    "${library_root}/lang/sql/adapter"
    "${library_root}/lang/sql/generated"
    "${CMAKE_CURRENT_SOURCE_DIR}/generated/${TARGET_PROCESSOR}"
    "${library_root}/src"
  )

  target_include_directories(thirdparty_berkeley-db SYSTEM INTERFACE
    "${CMAKE_CURRENT_SOURCE_DIR}/generated/${TARGET_PROCESSOR}"
    "${library_root}/src"
  )
endfunction()

berkeleydbMain()
